<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: vertx/net.js</title>
    
    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">
    
    <h1 class="page-title">Source: vertx/net.js</h1>
    
    


    
    <section>
        <article>
            <pre class="prettyprint source"><code>/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

if (typeof __vertxload === 'string') {
  throw "Use require() to load Vert.x API modules"
}

/**
 * The &lt;code>vertx/net&lt;/code> module provides network client and server
 * functions and classes.
 * @exports vertx/net
 */
var net = {};

load("vertx/ssl_support.js");
load("vertx/tcp_support.js");
load("vertx/read_stream.js");
load("vertx/write_stream.js");
load("vertx/helpers.js");

/**
 * Creates a {@linkcode module:vertx/net.NetServer|NetServer}.
 * @returns {module:vertx/net.NetServer} A new server instance
 */
net.createNetServer = function() {
  return new net.NetServer();
}

/**
 * Creates a {@linkcode module:vertx/net.NetClient|NetClient}.
 * @returns {module:vertx/net.NetClient} A new client instance
 */
net.createNetClient = function() {
  return new net.NetClient();
}

/**
 * A &lt;code>ConnectHandler&lt;/code> is a {@linkcode Handler} that accepts a
 * {@linkcode module:vertx/net.NetSocket} as it's parameter.
 * @typedef {function} ConnectHandler
 * @param {module:vertx/net.NetSocket} netSocket The raw socket
 */

/**
 * Represents a TCP or SSL server.  
 * @constructor
 * @mixes SSLSupport
 * @mixes ServerSSLSupport
 * @mixes TCPSupport
 * @mixes ServerTCPSupport
 */
net.NetServer = function() {
  var that = this;
  var jserver = __jvertx.createNetServer();

  sslSupport(this, jserver);
  serverSslSupport(this, jserver);
  tcpSupport(this, jserver);
  serverTcpSupport(this, jserver);

  /**
   * Supply a connect handler for this server. The server can only have at most
   * one connect handler at any one time.  As the server accepts TCP or SSL
   * connections it creates an instance of {@linkcode module:vertx/net.NetSocket}
   * and passes it to the connect handler.
   *
   * @param {ConnectHandler} handler the connection handler
   * @returns {module:vertx/net.NetServer} this
   */
  this.connectHandler = function(handler) {
    jserver.connectHandler(function(result) {
      handler(new net.NetSocket(result));
    });
    return that;
  }

  /**
   * Tell the server to start listening on all available interfaces and
   * &lt;code>port&lt;/code>. Be aware this is an async operation and the server may
   * not bound on return of the method.
   *
   * @param {number} port The network port to listen on
   * @param {string} [host] The hostname or IP address to listen on
   * @param {ConnectHandler} [handler] The connection handler
   * @returns {module:vertx/net.NetServer} this
   */
  this.listen = function() {
    var args = Array.prototype.slice.call(arguments);
    var handler = getArgValue('function', args);
    var host = getArgValue('string', args);
    var port = getArgValue('number', args);
    if (handler != null) {
      handler = adaptAsyncResultHandler(handler);
    }
    if (host == null) {
      host = "0.0.0.0";
    }
    jserver.listen(port, host, handler);
    return that;
  }

  /**
   * Close the server. This will close any open connections.
   * @param {Handler} [handler] the handler to call when the close operation has completed
   */
  this.close = function(handler) {
    if (handler === undefined) {
      jserver.close();
    } else {
      jserver.close(adaptAsyncResultHandler(handler));
    }
  }

  /**
   * The actual port the server is listening on. This is useful if you bound
   * the server specifying 0 as port number signifying an ephemeral port.
   * @returns {number} The port number the server is listening on
   */
  this.port = function() {
    return jserver.port();
  }

  /**
   * The host name
   * @returns {string} the host
   */
  this.host = function() {
    return jserver.host();
  }
}

/**
 * &lt;p>
 * A TCP/SSL client.
 * &lt;/p>
 * &lt;p>
 * Multiple connections to different servers can be made using the same
 * instance.  This client supports a configurable number of connection attempts
 * and a configurable delay between attempts.
 * &lt;/p>
 *
 * @constructor
 * @mixes sslSupport~SSLSupport
 * @mixes clientSslSupport~ClientSSLSupport
 * @mixes tcpSupport~TCPSupport
 */
net.NetClient = function() {
  var jclient = __jvertx.createNetClient();
  var that = this;
  sslSupport(this, jclient);
  clientSslSupport(this, jclient);
  tcpSupport(this, jclient);

  /**
   * Attempt to open a connection to a server at the specific port and host.
   * The connect is done asynchronously and on success, a
   * {@linkcode module:vertx/net.NetSocket} instance is supplied via the
   * {@linkcode ConnectHandler} instance 
   *
   * @param {number} port The port to connect on
   * @param {string} [host] The hostname or IP address to connect to (default: localhost)
   * @param {ConnectHandler} [connectHandler] The handler receiving the connection
   * @return {module:vertx/net.NetClient} this
   */
  this.connect = function(arg0, arg1, arg2) {
    var port = arg0;
    var host;
    var handler;
    if (arg2 === undefined) {
      host = 'localhost';
      handler = arg1;
    } else {
      host = arg1;
      handler = arg2;
    }
    jclient.connect(port, host, adaptAsyncResultHandler(handler, function(result) {
      return new net.NetSocket(result);
    }));
    return that;
  }

  /**
   * Get or set the number of reconnection attempts. Default value is zero.
   * @param {number} [attempts] the number of reconnection attempts to make before failing
   * @returns {number|module:vertx/net.NetClient}
   */
  this.reconnectAttempts = function(attempts) {
    if (attempts === undefined) {
      return jclient.getReconnectAttempts();
    } else {
      jclient.setReconnectAttempts(attempts);
      return that;
    }
  }

  /**
   * Get or set the reconnection interval, in milliseconds.
   * @param {number} [interval] the number of milliseconds to wait before attempting a reconnection
   * @returns {number|module:vertx/net.NetClient}
   */
  this.reconnectInterval = function(interval) {
    if (interval === undefined) {
      return jclient.getReconnectInterval();
    } else {
      jclient.setReconnectInterval(interval);
      return that;
    }
  }
  
  /**
   * Get or set the connect timeout, in milliseconds.
   * @param {number} [timeout] the number of milliseconds to wait before timing out a connection
   * @returns {number|module:vertx/net.NetClient}
   */
  this.connectTimeout = function(timeout) {
    if (timeout === undefined) {
      return jclient.getConnectTimeout();
    } else {
      jclient.setConnectTimeout(timeout);
      return that;
    }
  }
  
  /**
   * Close the client.
   */
  this.close = function() {
    jclient.close();
  }
}

/**
 * &lt;p>
 * A socket-like interface to a TCP/SSL connection on either the client or the
 * server side.
 * &lt;/p>
 * &lt;p>
 * Instances of this class are created on the client side by a 
 * {@linkcode module:vertx/net.NetClient} when a connection to a server is
 * made, or on the server side by a {@linkcode vertx/net.NetServer}
 * when a server accepts a connection.
 * &lt;/p>
 * &lt;p>
 * It implements both {@linkcode ReadStream} and 
 * {@linkcode WriteStream} so it can be used with 
 * {@linkcode module:vertx/pump~Pump} to pump data with flow control.
 * &lt;/p>
 * @constructor
 * @mixes ReadStream
 * @mixes WriteStream
 */
net.NetSocket = function(jNetSocket) {
  var that = this;
  readStream(this, jNetSocket);
  writeStream(this, jNetSocket);

  /**
   * &lt;p>
   * When a NetSocket is created it automatically registers an event handler
   * with the event bus, the ID of that handler is given by
   * writeHandlerID.
   * &lt;/p>
   * &lt;p>
   * Given this ID, a different event loop can send a buffer to that event
   * handler using the event bus and that buffer will be received by this
   * instance in its own event loop and written to the underlying connection.
   * This allows you to write data to other connections which are owned by
   * different event loops.
   * &lt;/p>
   */
  this.writeHandlerID = function() {
    return jNetSocket.writeHandlerID();
  }

  /**
   * Write a string or {@linkcode module:vertx/buffer~Buffer} to the socket.
   * @param {string|module:vertx/buffer~Buffer} chunk the data to write
   * @param {string} [encoding] the charset for the data (default: UTF-8)
   * @returns {module:vertx/net.NetSocket} this
   */
  this.write = function(arg0, arg1) {
    if (arg1 === undefined) {
      jNetSocket.write(arg0);
    } else {
      jNetSocket.write(arg0, arg1);
    }
    return that;
  }

  /**
   * Tell the kernel to stream a file as specified by &lt;code>filename&lt;/code>
   * directly from disk to the outgoing connection, bypassing userspace
   * altogether (where supported by the underlying operating system. This is a
   * very efficient way to stream files
   * @param {string} filename The path to the file
   * @returns {module:vertx/net.NetSocket} this
   */
  this.sendFile = function(filename) {
    jNetSocket.sendFile(filename);
    return that;
  }
  
  /**
   * Returns the remote address for this socket
   * @returns {Address} The remote address
   */
  this.remoteAddress = function() {
    return {
      'ipaddress': jNetSocket.remoteAddress().getAddress().getHostAddress(),
      'port': jNetSocket.remoteAddress().getPort()
    };
  }

  /**
   * Returns the local address for this socket
   * @returns {Address} The local address
   */
  this.localAddress = function() {
    return {
      'ipaddress': jNetSocket.localAddress().getAddress().getHostAddress(),
      'port': jNetSocket.localAddress().getPort()
    };
  }

  /**
   * Close this socket.
   */
  this.close = function() {
    jNetSocket.close();
  }

  /**
   * Set a {@linkcode Handler} to be called when this socket is closed
   * @param {Handler} handler The close handler to use
   */
  this.closeHandler = function(handler) {
    jNetSocket.closeHandler(handler);
    return that;
  }
}

/**
 * @typedef {{}} Address
 * @property {string} ipaddress The IP address
 * @property {number} port The port for this address
 */

module.exports = net;
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-vertx.html">vertx</a></li><li><a href="buffer.html">vertx/buffer</a></li><li><a href="console.html">vertx/console</a></li><li><a href="container.html">vertx/container</a></li><li><a href="event_bus.html">vertx/event_bus</a></li><li><a href="file_system.html">vertx/file_system</a></li><li><a href="http.html">vertx/http</a></li><li><a href="multi_map.html">vertx/multi_map</a></li><li><a href="net.html">vertx/net</a></li><li><a href="parse_tools.html">vertx/parse_tools</a></li><li><a href="pump.html">vertx/pump</a></li><li><a href="shared_data.html">vertx/shared_data</a></li><li><a href="sockjs.html">vertx/sockjs</a></li><li><a href="timer.html">vertx/timer</a></li></ul><h3>Externals</h3><ul><li><a href="InetSocketAddress.html">InetSocketAddress</a></li><li><a href="Buffer_.html">Buffer</a></li><li><a href="RecordParser.html">RecordParser</a></li><li><a href="SockJSSocket.html">SockJSSocket</a></li></ul><h3>Classes</h3><ul><li><a href="buffer-Buffer.html">Buffer</a></li><li><a href="file_system.AsyncFile.html">AsyncFile</a></li><li><a href="http.HttpClient.html">HttpClient</a></li><li><a href="http.HttpClientRequest.html">HttpClientRequest</a></li><li><a href="http.HttpClientResponse.html">HttpClientResponse</a></li><li><a href="http.HttpServer.html">HttpServer</a></li><li><a href="http.HttpServerFileUpload.html">HttpServerFileUpload</a></li><li><a href="http.HttpServerRequest.html">HttpServerRequest</a></li><li><a href="http.HttpServerResponse.html">HttpServerResponse</a></li><li><a href="http.RouteMatcher.html">RouteMatcher</a></li><li><a href="http.WebSocket.html">WebSocket</a></li><li><a href="multi_map-MultiMap.html">MultiMap</a></li><li><a href="net.NetClient.html">NetClient</a></li><li><a href="net.NetServer.html">NetServer</a></li><li><a href="net.NetSocket.html">NetSocket</a></li><li><a href="pump-Pump.html">Pump</a></li><li><a href="sockjs.SockJSServer.html">SockJSServer</a></li></ul><h3>Mixins</h3><ul><li><a href="ClientSSLSupport.html">ClientSSLSupport</a></li><li><a href="ReadStream.html">ReadStream</a></li><li><a href="ServerSSLSupport.html">ServerSSLSupport</a></li><li><a href="ServerTCPSupport.html">ServerTCPSupport</a></li><li><a href="SSLSupport.html">SSLSupport</a></li><li><a href="TCPSupport.html">TCPSupport</a></li><li><a href="WriteStream.html">WriteStream</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>

<br clear="both">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.0-dev</a> on Wed Jul 17 2013 20:27:22 GMT+0100 (BST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>